package com.example.mymusicdisplay.view.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.data.music.Songs;
import com.example.mymusicdisplay.databinding.RankingItemBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * author: ts
 * created on: 2021/10/9 下午4:51
 * description:
 */
public class RankingAdapter extends RecyclerView.Adapter<RankingAdapter.MyViewHolder> {
    private static final String TAG = "LocalMusicAdapter";

    private List<Songs> mMusicDataList = new ArrayList<>();

    private Context mContext;
    private RankingItemBinding mRankingItemBinding;


    public RankingAdapter(Context context) {
        mContext = context;
    }

    public void setMusicDataList(List<Songs> musicDataList) {
        Log.d(TAG, "setMusicDataList: ");
        if (musicDataList != null) {
            mMusicDataList.clear();
            mMusicDataList.addAll(musicDataList);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RankingAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mRankingItemBinding = DataBindingUtil
            .inflate(LayoutInflater.from(mContext), R.layout.ranking_item, parent, false);
        return new MyViewHolder(mRankingItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RankingAdapter.MyViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            Log.d(TAG, "onBindViewHolder: ");
            holder.binding(mMusicDataList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mMusicDataList == null ? 0 : mMusicDataList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        RankingItemBinding mBinding;

        public MyViewHolder(@NonNull RankingItemBinding rankingItemBinding) {
            super(rankingItemBinding.getRoot());
            mBinding = rankingItemBinding;
        }

        public void binding(Songs musicData) {
            Log.d(TAG, "binding: musicData = " + musicData);
            mBinding.rankingName.setText(musicData.getName());
        }
    }
}
